﻿//==================================================================
// Copyright (C) 2011 深圳市中旭企业管理股份有限公司
// 文件名: SelfDefineTree.cs
// 作 者：张骁晓
// 日 期：2012-03-15 09:37
// 描 述：根据传入的类型创建不同的类型树
// 版 本：1.00
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容
// 1.00             
//==================================================================
using System;
using System.Collections.Generic;
using System.Text;
using ZX.BLL.Extend;
using System.Web.UI.WebControls;
using System.ComponentModel;
using ZX.BLL;
using ZX.SYS.Library;
using ZX.CRM.Library;
using ZX.EHR.Library;
using System.Data;
namespace ZX.Web.UI.Controls
{
    public class SelfDefineTree : Literal
    {
        //类型
        private string _TreeType = "";
        [Description("需要创建的树的类型")]
        public string TreeType
        {
            get { return _TreeType; }
            set { _TreeType = value; }
        }
        private bool _isAllowCheck = false;
        [Description("是否允许多选")]
        public bool AllowCheck
        {
            get { return _isAllowCheck; }
            set { _isAllowCheck = value; }
        }

        BaseBussnies bll = new BaseBussnies();
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            string content = "";
            if (!DesignMode)
            {
                switch (TreeType.ToLower())
                {
                    case "course": content = CreateCourseTree(); //课程树
                        break;
                    case "productlayout": content = CreateProductLayoutTree();
                        break;
                    case "datpost": content = CreatePostTree();
                        break;
                    case "yearmonth":
                        content = CreateYearMonthTree("exec sp_Year_Month_Tree ' select StartTime from V_CrmMajorStudentHr where DliverState<>''73DAC2CE-F4BF-403C-9ACC-D075551E3328'''", "yearmonth");
                        break;
                    case "courseyearmonth":
                        content = CreateYearMonthTree("exec sp_Year_Month_Tree ' SELECT BeginDate FROM V_bpm_OpenClassBudgetMaster'", "courseyearmonth");
                        break;
                    case "term": content = CreateYearMonthTree("exec sp_Year_Month_Tree ' select StartTime from V_CrmDeliverHrTerm'", "term");
                        break;
                    case "class": content = CreateYearMonthTree("exec sp_Year_Month_Tree ' select StartTime from V_CrmDeliverHrClass'", "class");
                        break;
                }
            }
            writer.Write(content);
            base.Render(writer);
        }
        #region 创建职位树
        private string CreatePostTree()
        {
            //得到课程类型
            StringBuilder _strHtml = new StringBuilder();
            DatPost eDatPost = new DatPost();
            BaseBussnies<DatPost> postMgr = new BaseBussnies<DatPost>();
            postMgr.entity = eDatPost;
            IList<DatPost> ilist = bll.GetEntitys<DatPost>("ParentID='" + Guid.Empty.ToString() + "'");
            if (AllowCheck)
                _strHtml.Append("<div  id='" + this.ID + "' class='whitebg ygtv-checkbox'>");
            else
                _strHtml.Append("<div  id='" + this.ID + "' class='whitebg'>");
            _strHtml.Append("<ul>");
            _strHtml.Append("<li>职位列表");
            if (ilist != null && ilist.Count > 0)
            {
                _strHtml.Append("<ul>");
                //添加顶级职位节点
                foreach (DatPost v in ilist)
                {
                    _strHtml.Append("<li>");
                    _strHtml.Append(v.PostName);
                    //添加子职位节点
                    addSubPost(v.PostID, _strHtml);
                    _strHtml.Append("</li>");
                }
                _strHtml.Append("</ul>");
            }
            _strHtml.Append("</li></ul>");
            _strHtml.Append("</div>");
            return _strHtml.ToString();
        }
        //添加子职位节点
        private void addSubPost(Guid rootPostId, StringBuilder sb)
        {
            IList<DatPost> ilist = bll.GetEntitys<DatPost>("ParentID='" + rootPostId + "'");
            if (ilist != null && ilist.Count > 0)
            {
                sb.Append("<ul>");
                foreach (DatPost v in ilist)
                {
                    sb.Append("<li>");
                    sb.Append("<span><a href=\"#" + v.PostID + "\" style='display:block;cursor:hand' onclick=\"Native('" + v.PostID + "','" + v.PostID + "')\">" + v.PostID + "</a></span>");
                    sb.Append("</li>");
                }
                sb.Append("</ul>");
            }
        }


        #endregion

        #region 创建产品树
        private string CreateCourseTree()
        {
            //得到课程类型
            StringBuilder _strHtml = new StringBuilder();
            BDatPhraseDetail productTypeMgr = new BDatPhraseDetail();
            IList<DatPhraseDetail> ilist = productTypeMgr.GetDatPhraseDetailByClassValue("ProductType");
            if (AllowCheck)
                _strHtml.Append("<div  id='" + this.ID + "' class='whitebg ygtv-checkbox'>");
            else
                _strHtml.Append("<div  id='" + this.ID + "' class='whitebg'>");
            _strHtml.Append("<ul>");
            _strHtml.Append("<li>产品类型");
            if (ilist != null && ilist.Count > 0)
            {
                _strHtml.Append("<ul>");
                //添加产品类型
                foreach (DatPhraseDetail v in ilist)
                {
                    _strHtml.Append("<li>");
                    _strHtml.Append(v.PhraseText);
                    //添加产品节点
                    addCourse(v.PhraseID, _strHtml);
                    _strHtml.Append("</li>");
                }
                _strHtml.Append("</ul>");
            }
            _strHtml.Append("</li></ul>");
            _strHtml.Append("</div>");
            return _strHtml.ToString();

        }
        //添加产品节点到产品树
        private void addCourse(Guid productTypeId, StringBuilder sb)
        {
            BaseBussnies bll = new BaseBussnies();
            IList<CrmProduct> ilist = bll.GetEntitys<CrmProduct>("TypeId='" + productTypeId + "'");
            if (ilist != null && ilist.Count > 0)
            {
                sb.Append("<ul>");
                foreach (CrmProduct v in ilist)
                {
                    sb.Append("<li>");
                    sb.Append("<span><a href=\"#" + v.Productid + "\" style='display:block;cursor:hand' onclick=\"Native('" + v.Caption + "','" + v.Productid + "')\">" + v.Caption + "</a></span>");
                    sb.Append("</li>");
                }
                sb.Append("</ul>");
            }
        }

        #endregion

        #region 创建产品规划树
        private string CreateProductLayoutTree()
        {
            StringBuilder _strHtml = new StringBuilder();
            if (AllowCheck)
                _strHtml.Append("<div  id='" + this.ID + "' class='whitebg ygtv-checkbox'>");
            else
                _strHtml.Append("<div  id='" + this.ID + "' class='whitebg'>");
            _strHtml.Append("<ul>");
            _strHtml.Append("<li>年份");

            _strHtml.Append("<ul>");
            addYear(_strHtml);
            _strHtml.Append("</ul>");

            _strHtml.Append("</li></ul>");
            _strHtml.Append("</div>");
            return _strHtml.ToString();
        }
        private void addYear(StringBuilder sb)
        {
            BDatPhraseDetail productTypeMgr = new BDatPhraseDetail();
            //得到课程类型
            IList<DatPhraseDetail> ilist = productTypeMgr.GetDatPhraseDetailByClassValue("ProductType");

            DataTable dt = bll.GetDataBySqlComm("select distinct Left(CONVERT(nvarchar(50),BeginDate,121),4) from CrmProductLayout");
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    sb.Append("<li>");
                    sb.Append(dt.Rows[i][0].ToString());
                    if (ilist != null && ilist.Count > 0)
                    {
                        sb.Append("<ul>");
                        //添加产品类型
                        foreach (DatPhraseDetail v in ilist)
                        {
                            //添加产品节点
                            if (v.PhraseID.ToString().ToLower() == "9b1639ba-d3fd-4f1c-bab2-12fedbc8b30d") //培训类
                            {
                                addAllProduct(v.PhraseID, dt.Rows[i][0].ToString(), sb);
                            }
                        }
                        sb.Append("</ul>");
                    }
                    sb.Append("</li>");
                }
            }
            /*BCrmProductLayout bll = new BCrmProductLayout();
            DataTable dt = bll.GetProductLayoutYear();
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    sb.Append("<li>");
                    sb.Append(dt.Rows[i][0].ToString());
                    if (ilist != null && ilist.Count > 0)
                    {
                        sb.Append("<ul>");
                        //添加产品类型
                        foreach (DatPhraseDetail v in ilist)
                        {
                            //添加产品节点
                            if (v.PhraseID.ToString() == "9b1639ba-d3fd-4f1c-bab2-12fedbc8b30d") //培训类
                            {
                                addAllProduct(v.PhraseID, dt.Rows[i][0].ToString(), sb);
                            }
                        }
                        sb.Append("</ul>");
                    }
                    sb.Append("</li>");
                }
            }*/
        }
        private void addAllProduct(Guid productTypeId, string year, StringBuilder sb)
        {
            //Modify by hexingxiang (控制权限) 
            BasePage basePage = new BasePage();
            string UserId = basePage.UserId.ToString();
            if (basePage.EntityName == "V_CrmDeliverMajorStudent")  //课程排位表管理
            {
                if (UserId.ToUpper() == "F20F53A6-080B-4472-90A6-69E22D27317C") //系统管理员
                {
                    AddProduct(productTypeId, year, sb);
                }
                else
                {
                    //获取所有产品
                    //BCrmProductLayout bll = new BCrmProductLayout();
                    //IList<V_CrmProductLayout> ilist = bll.GetProductLayout(productTypeId, new Guid(UserId));
                    IList<V_CrmProductLayout> ilist = bll.GetEntitysBySqlComm<V_CrmProductLayout>("select distinct [Caption],[ProductID],[TypeId],[UserId] " +
                                                                        " from V_CrmProductLayout where TypeId='" + productTypeId + "' and UserId='" + UserId + "'" +
                                                                        " group by [BeginDate],[Caption],[ProductID],[TypeId],[UserId]");
                    if (ilist != null && ilist.Count > 0)
                    {
                        foreach (V_CrmProductLayout v in ilist)
                        {
                            sb.Append("<li>");
                            sb.Append(v.Caption);
                            addProductLayout(v.ProductID, year, sb);
                            sb.Append("</li>");
                        }
                    }
                }
            }
            else
            {
                AddProduct(productTypeId, year, sb);
            }

        }
        private void AddProduct(Guid productTypeId, string year, StringBuilder sb)
        {
            IList<CrmProduct> ilist = bll.GetEntitys<CrmProduct>("TypeId='" + productTypeId + "'");
            if (ilist != null && ilist.Count > 0)
            {
                foreach (CrmProduct v in ilist)
                {
                    sb.Append("<li>");
                    sb.Append(v.Caption);
                    addProductLayout(v.Productid, year, sb);
                    sb.Append("</li>");
                }
            }
        }
        //添加产品节点到产品树
        private void addProductLayout(Guid productId, string year, StringBuilder sb)
        {
            //Modify by hexingxiang (控制权限)
            BasePage basePage = new BasePage();
            string UserId = basePage.UserId.ToString();
            if (basePage.EntityName == "V_CrmDeliverMajorStudent")
            {
                if (UserId.ToUpper() == "F20F53A6-080B-4472-90A6-69E22D27317C") //系统管理员
                {
                    AddCrmProductLayout(productId, year, sb);
                }
                else
                {
                    DateTime BeginDate = DateTime.Now.AddDays(6);
                    IList<V_CrmProductLayout> ilist1 = bll.GetEntitys<V_CrmProductLayout>("ProductID='" + productId + "' and UserId='" + UserId + "' and BeginDate like '%" + year + "%' order by BeginDate desc");
                    if (ilist1 != null && ilist1.Count > 0)
                    {
                        sb.Append("<ul>");
                        foreach (V_CrmProductLayout v1 in ilist1)
                        {
                            if (v1.ProductRoleID.ToString() != "00000000-0000-0000-0000-000000000000") //不是设置的
                            {
                                if (v1.EndDate.AddDays(6) >= DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd")))
                                {
                                    sb.Append("<li>");
                                    sb.Append("<span><a href=\"#" + v1.ID + "\" style='display:block;cursor:hand' onclick=\"Native('" + v1.CourseLayoutName + "','" + v1.ProductLayoutId + "')\">" + v1.CourseLayoutName + "</a></span>");
                                    sb.Append("</li>");
                                }
                            }
                            else
                            {
                                sb.Append("<li>");
                                sb.Append("<span><a href=\"#" + v1.ID + "\" style='display:block;cursor:hand' onclick=\"Native('" + v1.CourseLayoutName + "','" + v1.ProductLayoutId + "')\">" + v1.CourseLayoutName + "</a></span>");
                                sb.Append("</li>");
                            }
                        }
                        sb.Append("</ul>");
                    }
                }
            }
            else
            {
                AddCrmProductLayout(productId, year, sb);
            }
        }
        private void AddCrmProductLayout(Guid productId, string year, StringBuilder sb)
        {
            IList<CrmProductLayout> ilist = bll.GetEntitys<CrmProductLayout>("ProductID='" + productId + "' and BeginDate like '%" + year + "%' order by BeginDate desc");
            if (ilist != null && ilist.Count > 0)
            {
                sb.Append("<ul>");
                foreach (CrmProductLayout v in ilist)
                {
                    sb.Append("<li>");
                    sb.Append("<span><a href=\"#" + v.ID + "\" style='display:block;cursor:hand' onclick=\"Native('" + v.CourseLayoutName + "','" + v.ID + "')\">" + v.CourseLayoutName + "</a></span>");
                    sb.Append("</li>");
                }
                sb.Append("</ul>");
            }
        }
        #endregion

        #region 创建年月树
        private string CreateYearMonthTree(string strSql, string types)
        {
            IList<CrmYearMonthTree> cym = bll.GetEntitysBySqlComm<CrmYearMonthTree>(strSql);
            StringBuilder _strHtml = new StringBuilder();
            _strHtml.Append("<div  id='" + this.ID + "' class='whitebg'>");
            _strHtml.Append("<ul>");
            _strHtml.Append("<li><span><a href=\"#\" style='display:block;cursor:hand' onclick=\"Native('yearAll','')\">年份</a></span>");
            if (cym != null && cym.Count > 0)
            {
                _strHtml.Append("<ul>");
                //添加年
                foreach (CrmYearMonthTree v in cym)
                {
                    if (v.Lev == 1)
                    {
                        _strHtml.Append("<li>");
                        _strHtml.Append("<span><a href=\"#\" style='display:block;cursor:hand' onclick=\"Native('year','" + v.Writer + "')\">" + v.Writer + "年</a></span>");
                        _strHtml.Append("<ul>");
                        AddMonth(v.ID, v.Writer, _strHtml, strSql, types);
                        _strHtml.Append("</ul>");
                        _strHtml.Append("</li>");
                    }
                }
                _strHtml.Append("</ul>");
            }
            _strHtml.Append("</li></ul>");
            _strHtml.Append("</div>");
            return _strHtml.ToString();
        }
        private void AddMonth(int parentid, int year, StringBuilder sb, string strSql, string types)
        {
            IList<CrmYearMonthTree> cym = bll.GetEntitysBySqlComm<CrmYearMonthTree>(strSql);
            foreach (CrmYearMonthTree cv in cym)
            {
                if (cv.Parentid == parentid) //月
                {
                    sb.Append("<li>");
                    sb.Append("<span><a href=\"#" + cv.ID + "\" style='display:block;cursor:hand' onclick=\"Native('" + year + "','" + cv.Writer + "')\">" + cv.Writer + "月</a></span>");
                    if (types == "yearmonth")
                    {
                        sb.Append("<ul>");
                        AddTerm(sb, year, cv.Writer);//增加期数
                        sb.Append("</ul>");
                    }
                    else if (types == "courseyearmonth")
                    {
                        sb.Append("<ul>");
                        AddCourseTerm(sb, year, cv.Writer);//增加课程
                        sb.Append("</ul>");
                    }
                    sb.Append("</li>");
                }
            }
        }
        /// <summary>
        /// 增加期数
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="year"></param>
        /// <param name="month"></param>
        private void AddTerm(StringBuilder sb, int year, int month)
        {
            IList<CrmDeliverHrTerm> cdh = bll.GetEntitysBySqlComm<CrmDeliverHrTerm>("select * from CrmDeliverHrTerm where datepart(yy,starttime)=" + year + " and datepart(mm,starttime)=" + month);
            foreach (var c in cdh)
            {
                sb.Append("<li>");
                sb.Append("<span><a href=\"#" + c.ID + "\" style='display:block;cursor:hand' onclick=\"Native('term','" + c.TermName + "')\">" + c.TermName + "期</a></span>");
                sb.Append("</li>");
            }
        }
        /// <summary>
        /// 增加课程
        /// </summary>
        /// <param name="sb"></param>
        /// <param name="year"></param>
        /// <param name="month"></param>
        private void AddCourseTerm(StringBuilder sb, int year, int month)
        {
            IList<V_bpm_OpenClassBudgetMaster> cdh = bll.GetEntitysBySqlComm<V_bpm_OpenClassBudgetMaster>("select distinct txtProductLayoutID from V_bpm_OpenClassBudgetMaster where datepart(yy,begindate)=" + year + " and datepart(mm,begindate)=" + month);
            foreach (var c in cdh)
            {
                sb.Append("<li>");
                sb.Append("<span><a href=\"#" + c.ID + "\" style='display:block;cursor:hand' onclick=\"Native('term','" + c.txtProductLayoutID + "')\">" + c.txtProductLayoutID + "</a></span>");
                sb.Append("</li>");
            }
        }
        #endregion
    }
}
